home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 October / macformat-005.iso / Shareware City / Developers / Incognito 1.2ß2 src Folder / Incognito ƒ / init / nbpaction.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-16  |  4.9 KB  |  280 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <PLStringFuncs.h>
  3. #include <Memory.h>
  4. #include <GestaltEqu.h>
  5. #include <time.h>
  6. #include <errors.h>
  7.  
  8. #include "Common.h"
  9.  
  10. extern OSType getGestalt(void);
  11.  
  12. static MyGestaltPtr GetGestaltInfo(void)
  13. {
  14.     register MyGestaltPtr    pb;
  15.     register OSErr            error;
  16.     
  17.     error = Gestalt(getGestalt(), (long *)&pb);
  18.     if (error) pb = 0;
  19.     return pb;
  20. }
  21.  
  22. static NameLinkPtr GetRegisteredNames(void)
  23. {
  24.     register NameLinkPtr pb = nil;
  25.     register MyGestaltPtr pc;
  26.     
  27.     pc = GetGestaltInfo();
  28.     if (pc)
  29.     {
  30.         pb = pc->registeredNames;
  31.     }
  32.     return pb;
  33. }
  34.  
  35. static OriginalLinkPtr GetMaskableNames(void)
  36. {
  37.     register OriginalLinkPtr    pb = nil;
  38.     register MyGestaltPtr        pc;
  39.     
  40.     pc = GetGestaltInfo();
  41.     if (pc)
  42.     {
  43.         pb = pc->trappedNames;
  44.     }
  45.     return pb;
  46. }
  47.  
  48. /*
  49.   Is the string one that we should mask out?
  50. */
  51.  
  52. static Boolean StringIsMaskable(char* theString)
  53. {
  54.     OriginalLinkPtr pb = GetMaskableNames();
  55.     char* maskString;
  56.     short result;
  57.  
  58.     if (!pb) return false;
  59.     
  60.     maskString = pb->originalName;
  61.  
  62.     while (true)
  63.     {
  64.         result = maskString ? !PLstrcmp(theString, maskString) : false;
  65.         if (result)
  66.         {
  67.             return true;
  68.         }
  69.         pb = pb->next;
  70.         if (!pb) break;
  71.         maskString = pb->originalName;
  72.     }
  73.     return false;
  74. }
  75.  
  76. /*
  77.   Has the string already been masked out?
  78. */
  79.  
  80. static Boolean IsStringMasked(char* theString)
  81. {
  82.     NameLinkPtr pb = GetRegisteredNames();
  83.     register char* registeredName;
  84.     short result;
  85.  
  86.     if (!pb) return false;
  87.     registeredName = pb->originalName;
  88.  
  89.     while (true)
  90.     {
  91.         result = registeredName ? !PLstrcmp(theString, registeredName) : false;
  92.         if (result)
  93.         {
  94.             return true;
  95.         }
  96.         pb = pb->next;
  97.         if (!pb) break;
  98.         registeredName = pb->originalName;
  99.     }
  100.     return false;
  101. }
  102.  
  103. /*
  104.   This is made 4 quark: it tries to deregister a string,
  105.   so if the string is already masked set the string passed
  106.   in to the mask.
  107. */
  108.  
  109. static void SetStringToMask(char* theString)
  110. {
  111.     NameLinkPtr pb = GetRegisteredNames();
  112.     char* registeredName;
  113.     short result;
  114.  
  115.     if (!pb) return;
  116.     registeredName = pb->originalName;
  117.  
  118.     while (true)
  119.     {
  120.         result = registeredName ? !PLstrcmp(theString, registeredName) : false;
  121.         if (result)
  122.         {
  123.             PLstrcpy(theString, pb->newString);
  124.             return;
  125.         }
  126.         pb = pb->next;
  127.         if (!pb) break;
  128.         registeredName = pb->originalName;
  129.     }
  130.     return;
  131. }
  132.  
  133. /*
  134.   Munge the string
  135. */
  136.  
  137. static void Munge(char* theString)
  138. {
  139.     register short i;
  140.     register char* j;
  141.     
  142.     j = theString;
  143.     i = theString[0];
  144.     while (i)
  145.     {
  146.         theString[i--] = clock() + i;
  147.     }
  148. }
  149.  
  150. /*
  151.   Insert the old & new strings into the mask list, & munge too
  152. */
  153.  
  154. static void MaskString(char* theString)
  155. {
  156.     NameLinkPtr pb = GetRegisteredNames();
  157.     NameLinkPtr newLink;
  158.     
  159.     if (!pb) return;
  160.  
  161.     newLink = (NameLinkPtr) NewPtrSysClear(sizeof(NameLink));
  162.     if (!newLink) return;
  163.     PLstrcpy(newLink->originalName, theString);
  164.     Munge(theString);
  165.     PLstrcpy(newLink->newString, theString);
  166.  
  167.     while (true)
  168.     {
  169.         if (pb->next)
  170.         {
  171.             pb = pb->next;
  172.         }
  173.         else
  174.         {
  175.             pb->next = newLink;
  176.             newLink->previous = pb;
  177.             return;
  178.         }
  179.     }
  180.  
  181. }
  182.  
  183. /*
  184.   removes an entry from the NameLink list
  185. */
  186.  
  187. static void RemoveEntry(char* theString)
  188. {
  189.     NameLinkPtr pb = GetRegisteredNames();
  190.     char* str;
  191.  
  192.     if (!pb || !theString || !theString[0]) return;
  193.     
  194.     str = pb->newString;
  195.     while (true)
  196.     {
  197.         if (str && str[0])
  198.         if (!PLstrcmp(str, theString))
  199.         {
  200.             if (pb->previous)
  201.             {
  202.                 (pb->previous)->next = pb->next;
  203.             }
  204.             if (pb->next)
  205.             {
  206.                 (pb->next)->previous = pb->previous;
  207.             }
  208.             DisposePtr((Ptr)pb);
  209.             return;
  210.         }
  211.         pb = pb->next;
  212.         if (!pb) break;
  213.         str = pb->newString;
  214.     }
  215.  
  216. }
  217.  
  218. // Basically, when registering something, we ask: is the object being deregistered
  219. // one of the ones we should mask out, and if so, has it already been munged?
  220. // If it was already munged, we should set the string to be deregistered to the
  221. // munged string, not the original string.
  222.  
  223. // Why? Because quark, for reasons unknown, registers, throws away the parameter
  224. // block, then deregisters the string that it was supposed to be registered as,
  225. // not the string it actually was registered as (like every other program).
  226.  
  227. void DoRemove(char* typeName, char* objName)
  228. {
  229.     if (typeName && typeName[0])
  230.     {
  231.         if (StringIsMaskable(typeName) && IsStringMasked(typeName))
  232.         {
  233.             SetStringToMask(typeName);
  234.         }
  235.         RemoveEntry(typeName);
  236.     }
  237.     
  238.     if (objName && objName[0])
  239.     {
  240.         if (StringIsMaskable(objName) && IsStringMasked(objName))
  241.         {
  242.             SetStringToMask(objName);
  243.         }
  244.         RemoveEntry(objName);
  245.     }
  246. }
  247.  
  248. static void CheckRegister(char* typeName)
  249. {
  250.     if (StringIsMaskable(typeName))                // should we worry about wiping the type?
  251.     {
  252.         if (!IsStringMasked(typeName))
  253.         {
  254.             MaskString(typeName);
  255.         }
  256.     }
  257. }
  258.  
  259. void DoRegister(char* typeName, char* objName)
  260. {
  261.     if (typeName && typeName[0]) CheckRegister(typeName);
  262.     if (objName && objName[0]) CheckRegister(objName);
  263. }
  264.  
  265. //    Also does lookups, since they're basically the same.
  266. void DoConfirm(char* typeName, char* objName)
  267. {
  268.     if (typeName && typeName[0])
  269.     if (StringIsMaskable(typeName))
  270.     {
  271.         Munge(typeName);
  272.     }
  273.     
  274.     if (objName && objName[0])
  275.     if (StringIsMaskable(objName))
  276.     {
  277.         Munge(objName);
  278.     }
  279. }
  280.